home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / programming / e / lsestuff / nmiovlist.e < prev    next >
Text File  |  1999-11-29  |  11KB  |  444 lines

  1. OPT MODULE
  2.  
  3. ->
  4. -> was : xliv, newarrayX, collectionX.. :)
  5. -> this is a listclass ..hmm.. or..
  6. -> its a class that uses nodes that have a value
  7. -> (besides id field ofcource)
  8. -> the class does things with the values .. :)
  9. -> alot arraylike functions
  10. -> and list <> list functions...
  11. -> order in the list, please!
  12. -> well.. automatically this list is ordered..
  13. -> alot functions to manipulate values depends on that
  14. -> and it makes that functions faaaster .. :)
  15.  
  16. MODULE 'leifoo/nm'
  17. MODULE '*nmIoList'
  18.  
  19. EXPORT OBJECT nmIoVList OF nmIoList
  20. ENDOBJECT
  21.  
  22.  
  23. EXPORT OBJECT nmIoVList_CPObj OF nmIoList_CPObj
  24. ENDOBJECT
  25.  
  26. PROC private_Methods_From_Here() OF nmIoVList IS self.o_bla('private_Methods_From_Here()')
  27.  
  28. PROC countNoNIL() OF nmIoVList
  29.    DEF n:REG PTR TO nmIV
  30.    DEF count:REG
  31.    count := NIL
  32.    n := self.first()
  33.    WHILE n
  34.       IF n.value <> NIL THEN INC count
  35.       n := n.next
  36.    ENDWHILE
  37. ENDPROC count
  38.  
  39. PROC absVals() OF nmIoVList
  40.    DEF n:PTR TO nmIV
  41.    DEF useful=NIL
  42.    n := self.first()
  43.    WHILE n
  44.       IF n.value < 0
  45.          n.value := Abs(n.value)
  46.          INC useful
  47.       ENDIF
  48.       n := n.next
  49.    ENDWHILE
  50. ENDPROC useful
  51.  
  52. PROC getSumVals() OF nmIoVList
  53.    DEF n:REG PTR TO nmIV
  54.    DEF sum:REG
  55.    sum := NIL
  56.    n := self.first()
  57.    WHILE n
  58.       sum := sum + n.value
  59.       n := n.next
  60.    ENDWHILE
  61. ENDPROC sum
  62.  
  63. PROC getAveVals() OF nmIoVList
  64.    DEF sum:REG
  65.    DEF count:REG
  66.    DEF n:REG PTR TO nmIV
  67.    sum := NIL
  68.    count := NIL
  69.    n := self.first()
  70.    WHILE n
  71.       INC count
  72.       sum := sum + n.value
  73.       n := n.next
  74.    ENDWHILE
  75.    IF count = NIL THEN RETURN NIL
  76. ENDPROC sum / count
  77.  
  78. PROC getMaxVal() OF nmIoVList
  79.    DEF n:REG PTR TO nmIV
  80.    DEF val:REG
  81.    val:=$80000000
  82.    n := self.first()
  83.    WHILE n
  84.       val := IF val < n.id THEN n.id ELSE val
  85.       n := n.next
  86.    ENDWHILE
  87. ENDPROC val
  88.  
  89. PROC getMinVal() OF nmIoVList
  90.    DEF n:REG PTR TO nmIV
  91.    DEF val:REG
  92.    val:=$40000000
  93.    n := self.first()
  94.    WHILE n
  95.       val := IF val > n.id THEN n.id ELSE val
  96.       n := n.next
  97.    ENDWHILE
  98. ENDPROC val
  99.  
  100.  
  101. /* needs sorted lists ! */
  102. PROC applyExistsFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  103.    DEF thisnode:REG PTR TO nmIV
  104.    DEF thatnode:REG PTR TO nmIV
  105.    DEF useful:REG
  106.    useful := NIL
  107.    thisnode := self.first()
  108.    thatnode := nmIoVList.first()
  109.    WHILE (thisnode AND thatnode)
  110.       IF thisnode.id = thatnode.id
  111.          thisnode.value := thatnode.value
  112.          thisnode := thisnode.next
  113.          thatnode := thatnode.next
  114.          INC useful
  115.       ELSEIF thisnode.id > thatnode.id
  116.          thatnode := thatnode.next
  117.       ELSE
  118.          thisnode := thisnode.next
  119.       ENDIF
  120.    ENDWHILE
  121. ENDPROC useful
  122.  
  123. PROC applyANDFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  124.    DEF thisnode:REG PTR TO nmIV
  125.    DEF thatnode:REG PTR TO nmIV
  126.    DEF useful:REG
  127.    useful := NIL
  128.    thisnode := self.first()
  129.    thatnode := nmIoVList.first()
  130.    WHILE (thisnode AND thatnode)
  131.       IF thisnode.id = thatnode.id
  132.          thisnode.value := thatnode.value AND thisnode.value
  133.          thisnode := thisnode.next
  134.          thatnode := thatnode.next
  135.          INC useful
  136.       ELSEIF thisnode.id > thatnode.id
  137.          thatnode := thatnode.next
  138.       ELSE
  139.          thisnode := thisnode.next
  140.       ENDIF
  141.    ENDWHILE
  142. ENDPROC useful
  143.  
  144. PROC applyNewFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  145.    DEF thisnode:PTR TO nmIV
  146.    DEF thatnode:PTR TO nmIV
  147.    DEF newnode:PTR TO nmIV
  148.    DEF hits=NIL
  149.    thisnode := self.first()
  150.    thatnode := nmIoVList.first()
  151.    WHILE thatnode
  152.       IF thisnode.id > thatnode.id
  153.          NEW newnode
  154.          newnode.id := thatnode.id
  155.          newnode.value := thatnode.value
  156.          self.oInsert(newnode)
  157.          thatnode := thatnode.next
  158.          INC hits
  159.       ELSEIF thisnode.id < thatnode.id
  160.          thisnode := thisnode.next
  161.       ELSE
  162.          thisnode := thisnode.next
  163.          thatnode := thatnode.next
  164.       ENDIF
  165.    ENDWHILE
  166. ENDPROC hits
  167.  
  168. PROC applyAllFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  169.    DEF thisnode:PTR TO nmIV
  170.    DEF thatnode:PTR TO nmIV
  171.    DEF newnode:PTR TO nmIV
  172.    thisnode := self.first()
  173.    thatnode := nmIoVList.first()
  174.    WHILE thatnode
  175.       IF thisnode.id > thatnode.id
  176.          NEW newnode
  177.          newnode.id := thatnode.id
  178.          newnode.value := thatnode.value
  179.          self.oInsert(newnode)
  180.          thatnode := thatnode.next
  181.       ELSEIF thisnode.id < thatnode.id
  182.          thisnode := thisnode.next
  183.       ELSE
  184.          thisnode.value := thatnode.value
  185.          thisnode := thisnode.next
  186.          thatnode := thatnode.next
  187.       ENDIF
  188.    ENDWHILE
  189. ENDPROC
  190.  
  191. PROC applyAveFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  192.    DEF thisnode:PTR TO nmIV
  193.    DEF thatnode:PTR TO nmIV
  194.    DEF newnode:PTR TO nmIV
  195.    thisnode := self.first()
  196.    thatnode := nmIoVList.first()
  197.    WHILE thatnode
  198.       IF thisnode.id > thatnode.id
  199.          NEW newnode
  200.          newnode.id := thatnode.id
  201.          newnode.value := (thatnode.value) / 2
  202.          self.ordInsert(newnode)
  203.          thatnode := thatnode.next
  204.       ELSEIF thisnode.id < thatnode.id
  205.          thisnode := thisnode.next
  206.       ELSE
  207.          thisnode.value := ((thatnode.value) + (thisnode.value)) / 2
  208.          thisnode := thisnode.next
  209.          thatnode := thatnode.next
  210.       ENDIF
  211.    ENDWHILE
  212. ENDPROC
  213.  
  214. PROC applyORFrom(nmIoVList:PTR TO nmIoVList) OF nmIoVList
  215.    DEF thisnode:PTR TO nmIV
  216.    DEF thatnode:PTR TO nmIV
  217.    DEF newnode:PTR TO nmIV
  218.    thisnode := self.first()
  219.    thatnode := nmIoVList.first()
  220.    WHILE thatnode
  221.       IF thisnode.id > thatnode.id
  222.          NEW newnode
  223.          newnode.id := thatnode.id
  224.          newnode.value := thatnode.value
  225.          self.oInsert(newnode)
  226.          thatnode := thatnode.next
  227.       ELSEIF thisnode.id < thatnode.id
  228.          thisnode := thisnode.next
  229.       ELSE
  230.          thisnode.value := thatnode.value OR thisnode.value
  231.          thisnode := thisnode.next
  232.          thatnode := thatnode.next
  233.       ENDIF
  234.    ENDWHILE
  235. ENDPROC
  236.  
  237. PROC set(id, value) OF nmIoVList
  238.    DEF n:PTR TO nmIV
  239.    n := self.oFind(id)
  240.    IF n = NIL
  241.       ->IF value = NIL THEN RETURN NIL
  242.       NEW n
  243.       n.id := id
  244.       n := self.oInsert(n)
  245.    ENDIF
  246.    n.value := value
  247. ENDPROC n
  248.  
  249. PROC get(id) OF nmIoVList
  250.    DEF n:PTR TO nmIV
  251.    n := self.oFind(id)
  252.    IF n = NIL THEN RETURN NIL
  253. ENDPROC n.value
  254.  
  255. PROC unSet(id) OF nmIoVList
  256.    DEF n:PTR TO nmIV
  257.    n := self.oFind(id)
  258.    IF n = NIL THEN RETURN NIL
  259.    self.delete(n)
  260. ENDPROC
  261.  
  262. PROC cleanUp() OF nmIoVList
  263.    DEF n:PTR TO nmIV
  264.    DEF next
  265.    n := self.first()
  266.    WHILE n
  267.       next := n.next
  268.       IF n.value = NIL THEN self.delete(n)
  269.       n := next
  270.    ENDWHILE
  271. ENDPROC
  272.  
  273. /* some shit for supporting the bulk getting.. */
  274. OBJECT blahaj OF nmIoVList_CPObj
  275.    arrayptr
  276. ENDOBJECT
  277.  
  278. PROC wblahaL(blahaj:PTR TO blahaj)
  279.    DEF l:PTR TO LONG
  280.    DEF n:PTR TO nmIV
  281.    l := blahaj.arrayptr
  282.    n := blahaj.node
  283.    l[] := n.value
  284.    blahaj.arrayptr := (blahaj.arrayptr) + 4
  285. ENDPROC
  286.  
  287. PROC wblahaI(blahaj:PTR TO blahaj)
  288.    DEF l:PTR TO INT
  289.    DEF n:PTR TO nmIV
  290.    l := blahaj.arrayptr
  291.    n := blahaj.node
  292.    l[] := n.value
  293.    blahaj.arrayptr := (blahaj.arrayptr) + 2
  294. ENDPROC
  295.  
  296. PROC wblahaC(blahaj:PTR TO blahaj)
  297.    DEF l:PTR TO CHAR
  298.    DEF n:PTR TO nmIV
  299.    l := blahaj.arrayptr
  300.    n := blahaj.node
  301.    l[] := n.value
  302.    blahaj.arrayptr := (blahaj.arrayptr) + 1
  303. ENDPROC
  304.  
  305. /* bulk set */
  306. PROC cBSet(array:PTR TO CHAR, startid, stopid) OF nmIoVList
  307.    WHILE startid <> (stopid + 1) DO self.set(startid++, array[]++)
  308. ENDPROC
  309.  
  310. PROC iBSet(array:PTR TO INT, startid, stopid) OF nmIoVList
  311.    WHILE startid <> (stopid + 1) DO self.set(startid++, array[]++)
  312. ENDPROC
  313.  
  314. PROC lBSet(array:PTR TO LONG, startid, stopid) OF nmIoVList
  315.    WHILE startid <> (stopid + 1) DO self.set(startid++, array[]++)
  316. ENDPROC
  317.  
  318. /* bulk get */
  319. /* *optmised* skips the get() method */
  320. /* more code.. damn..*/
  321. PROC cBGet(array:PTR TO CHAR, startid, stopid) OF nmIoVList
  322.    DEF b:PTR TO blahaj
  323.    b.arrayptr := array
  324.    self.travArray({wblahaC}, b, startid, stopid)
  325. ENDPROC
  326.  
  327. PROC iBGet(array:PTR TO CHAR, startid, stopid) OF nmIoVList
  328.    DEF b:PTR TO blahaj
  329.    b.arrayptr := array
  330.    self.travArray({wblahaI}, b, startid, stopid)
  331. ENDPROC
  332.  
  333. PROC lBGet(array:PTR TO CHAR, startid, stopid) OF nmIoVList
  334.    DEF b:PTR TO blahaj
  335.    b.arrayptr := array
  336.    self.travArray({wblahaL}, b, startid, stopid)
  337. ENDPROC
  338.  
  339. /* this one is pretty clever :) */
  340. /* should be pretty faast, comparing to set()/get():ing.. */
  341. PROC swapV(id1, id2) OF nmIoVList
  342.    DEF n1:REG PTR TO nmIV
  343.    DEF n2:REG PTR TO nmIV
  344.    DEF n:REG PTR TO nmIV
  345.    DEF temp
  346.    n1 := NIL
  347.    n2 := NIL
  348.    n := self.first()
  349.    WHILE n
  350.       IF n.id = id1 THEN n1 := n
  351.       IF n.id = id2 THEN n2 := n
  352.       n := IF (n1 AND n2) THEN NIL ELSE n.next
  353.    ENDWHILE
  354.    IF (n1 = NIL) AND (n2 = NIL) THEN RETURN NIL
  355.    IF n1 = NIL
  356.       n1 := createIVN(id1, 0)
  357.       self.oInsert(n1)
  358.    ENDIF
  359.    IF n2 = NIL
  360.       n2 := createIVN(id2, 0)
  361.       self.oInsert(n2)
  362.    ENDIF
  363.    temp := n1.value
  364.    n1.value := n2.value
  365.    n2.value := temp
  366. ENDPROC
  367.  
  368. /* simple bubblesorting of values.. */
  369. PROC sortV() OF nmIoVList
  370.    DEF n:REG PTR TO nmIV
  371.    DEF nnext:REG PTR TO nmIV
  372.    DEF useful:REG
  373.    DEF temp:REG
  374.    REPEAT
  375.       n := self.first()
  376.       useful := FALSE
  377.       WHILE n
  378.          nnext := n.next
  379.          IF nnext
  380.             IF n.value > nnext.value
  381.                temp := n.value
  382.                n.value := nnext.value
  383.                nnext.value := temp
  384.                useful := TRUE
  385.             ENDIF
  386.             n := n.next
  387.          ELSE
  388.             n := NIL
  389.          ENDIF
  390.       ENDWHILE
  391.    UNTIL useful = FALSE
  392. ENDPROC
  393.  
  394. /* this one is clever! :) */
  395. /* fakes nodes that doesnt exist */
  396. /* like it was a real array, not a list :) */
  397. PROC indexTraverse(proc, cpobj:PTR TO nmIoList_CPObj, startid, endid) OF nmIoVList
  398.    DEF n:PTR TO xni
  399.    DEF id
  400.    DEF defnode:nmIV
  401.    defnode.next := NIL
  402.    defnode.prev := NIL
  403.    defnode.value := NIL
  404.    cpobj.list := self
  405.    id := startid
  406.    INC endid
  407.    n := self.first()
  408.    WHILE id < endid
  409.       IF (n = NIL) OR (n.id > id)
  410.          defnode.id := id
  411.          cpobj.node := defnode
  412.          proc(cpobj)
  413.          INC id
  414.       ELSE
  415.          IF n.id < id
  416.             n := n.next
  417.          ELSE -> n.id = id
  418.             cpobj.node := n
  419.             proc(cpobj)
  420.             INC id
  421.          ENDIF
  422.       ENDIF
  423.    ENDWHILE
  424. ENDPROC
  425.  
  426. PROC cloneContentsTo(nax:PTR TO nmIoVList) OF nmIoVList
  427.    nax.clear()
  428.    self.cloneFastNew(nax, nax.getObjectSize())
  429. ENDPROC self.first()
  430.  
  431. PROC scrollX(amount) OF nmIoVList IS self.scroll(amount)
  432.  
  433. PROC cmpMapX(nax:PTR TO nmIoVList) OF nmIoVList IS self.cmpMap(nax)
  434.  
  435. PROC getMaxX() OF nmIoVList IS self.oGetMaxID()
  436.  
  437. PROC getMinX() OF nmIoVList IS self.oGetMinID()
  438.  
  439.  
  440. /*EE folds
  441. -1
  442. 5 1 7 2 9 1 15 8 18 10 21 8 24 12 27 8 30 8 33 4 36 1 40 18 43 18 46 21 49 20 52 20 55 20 58 9 61 3 64 4 67 1 70 6 73 2 75 6 78 6 81 6 84 1 87 1 90 1 93 3 96 3 99 3 102 24 105 22 110 26 113 2 
  443. EE folds*/
  444.